//文字违规检测
'use strict';
const request = require('request')
const config = require('../config')
var http = require('http');
var https = require('https');
var MsgCheck = {
    init(){
        // if(!this._init){
        //     this._init = true;
        //     let cron = require('node-cron');
        //     let self = this;
        //     cron.schedule('* 0 * * * *', async function(){
        //         self.updateAccessToken()
        //     });
        //     this.updateAccessToken();
        // }
        //
        // if(!this.accessToken){
        //     return;
        // }
        // this.checkText('操你妈');
    },

    async getMsg(_token,content) {
        return new Promise(function (resolve, reject) {

            var postData = {

                content: content

            };

            postData = JSON.stringify(postData)

            console.log('after')

            console.log(postData)

            var options = {

                hostname: 'api.weixin.qq.com',

                path: '/wxa/msg_sec_check?access_token=' + _token,

                port:443,

                method: 'POST',

                headers:{

                    'Content-Type': 'application/json',

                    'Content-Length': postData.length,

                }

            };

            var msg = '';

            const reqB = https.request(options, (res) => {

                console.log('STATUS: ' + res.statusCode);

                console.log('HEADERS: ' + JSON.stringify(res.headers));

                res.on('data', function (data) {

                    console.log('data')

                    msg += data;

                });

                res.on('end', function () {

                    console.log('end')

                    resolve(msg)

                });

            });

            reqB.write(postData);

            reqB.on('error', function (e){

                console.log('error')

                msg = e;

                resolve(msg)

            });

            reqB.end();

        })

    },



    async checkText(text,cb){
        // if(!this.accessToken){
        //     return cb(false);
        // }
        let url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='+this.accessToken+'&content='+text;
        // console.log('url:',url)
        let res = await this.getMsg('16_3QI9nWDTcM3_ul6Kqp7EBW6XcMe861SXDlCuc7UC6TvAqMkMYM9vSlzi80aW3jLgtS0-d69OiY2foKzD_i2jyvXSh6w_xahaGmhyvb5I5Uudy_ojdnDd0dzNrg_vWb7e6Z_B0UQLUA20ZVpxWMAeAGALBP',text)
        console.log('checkText:',res)
    },
    updateAccessToken(){
        let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='+config.weChat.appid+'&secret='+config.weChat.appsecret;
        let self = this;
        request(url,function(err,res,body){
            if(!err){
                try{
                    self.accessToken = JSON.parse(body).access_token
                    self.checkText('完2347全dfji试3726测asad感3847知qwez到',function(r){
                        console.log('检测结果:',r);
                    })
                }catch(e){
                    console.error('拉取后解析成json失败:',body,'\n重新拉取中');
                    self.updateAccessToken()
                }

            } else {
                console.error('未拉取到token');
            }
        })
    },
}

module.exports = MsgCheck